package com.hua.store.web.servlet;

import com.hua.store.domin.Category;
import com.hua.store.service.CategoryService;
import com.hua.store.service.impl.CategoryServiceImpl;
import com.hua.store.utils.JedisUtils;
import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet(name = "CategoryServlet", urlPatterns = "/CategoryServlet")
public class CategoryServlet extends BaseServlet {
    public String findAllCats(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        Jedis jedis = JedisUtils.getJedis();
        String json = jedis.get("cat");
        if (json == null || "".equals(json)) {
            CategoryService service = new CategoryServiceImpl();
            List<Category> list = service.getAllCats();
            json = JSONArray.fromObject(list).toString();
            jedis.set("cat", json);
//            System.out.println("redis中没有数据");
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().print(json);
        } else {
//            System.out.println("redis中有数据");
            resp.setContentType("application/json;charset=utf-8");
            resp.getWriter().print(json);
        }


        return null;
    }
}
